package com.maxatime.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.maxatime.exception.PhoenixException;
import com.maxatime.pojo.Classroom;
import com.maxatime.pojo.ClassroomVo;
import com.maxatime.service.ClassroomService;
import com.maxatime.utils.MyContent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author Mr.Zhao
 * @title: ClassroomController
 * @projectName phoenix
 * @description: TODO
 * @date 2021/7/26 10:30
 *
 */
@Controller
public class ClassroomController {
    Logger log= LogManager.getLogger();
    @Autowired
    private ClassroomService classroomService;

    @RequestMapping("/classroomFind")
    @ResponseBody
    public Map<String,Object> classroomFind(@RequestParam(required=false,defaultValue="1") int page,
                                          @RequestParam(required=false,defaultValue="7") int limit,
                                            ClassroomVo vo
                                          ){
        // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
        // 从他的下一条sql开始分页
        PageHelper.startPage(page, limit);
        List<Classroom> stu  = classroomService.classroomFind(vo);// 这是我们的sql
//        for(StudentMessage i:stu){
//            i.setNativePlace(i.getNativePlace());
//        }

//        log.info("data_______:::::"+stu);
//        log.info("String studentName, String mobile, String sex,Integer age,String idNum：：：：：：："
//                +studentName+"   "
//                +mobile+"   "
//                +sex+"   "
//                +age+"   "
//                +idNum+"   "
//        );
        // 使用pageInfo包装查询
        PageInfo<Classroom> rolePageInfo = new PageInfo<>(stu);//
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",rolePageInfo.getTotal());
        map.put("data",rolePageInfo.getList());
        log.info("map------------------------------------"+map);
        return map;
    }


    @RequestMapping("/classDelete")
    @ResponseBody
    public Integer studentDelete(@RequestBody String data){
        JSONObject jsonObject = JSONObject.parseObject(data);
        String pk = jsonObject.getString("pkId");
        int pkId = Integer.parseInt(pk);


        return classroomService.classDelete(pkId);
    }
    @ResponseBody
    @RequestMapping("/updateClass")
    public Map<String,Object> updateClass(@RequestBody String updDate){
        Map<String,Object> m=new HashMap<String,Object>();
        JSONObject jsonObject=JSONObject.parseObject(updDate);
        Classroom clas=new Classroom();
        clas.setPkId(Integer.parseInt(jsonObject.getString("pkId")));
        clas.setArea(jsonObject.getString("area"));
        clas.setBuildingNo(jsonObject.getString("buildingNo"));
        clas.setBuildingFloor(jsonObject.getString("buildingFloor"));
        clas.setClassroomNumber(jsonObject.getString("classroomNumber"));
        clas.setClassroomName(jsonObject.getString("classroomName"));
        clas.setGalleryful(Integer.parseInt(jsonObject.getString("galleryful")));
        clas.setUseStatus(jsonObject.getString("useStatus"));

        Calendar now = Calendar.getInstance();//获取时间
        clas.setUpdateTime(now.getTime());//当前时间

        try {
            Integer cc= classroomService.updateClass(clas);
            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"修改成功");
            m.put("data",cc);



        }catch (PhoenixException e){
            e.printStackTrace();
            log.error("exception::"+e.getMessage());
            log.error("exception--code"+e.getCode());
            log.error("exception--msg"+e.getMsg());
            m.put(MyContent.CODE,e.getCode());
            m.put(MyContent.MSG,e.getMsg());
            m.put("data",0);
        }

            return m;
    }


    @RequestMapping("/insertClass")
    @ResponseBody
    public Map<String,Object> insertClass(@RequestBody String istDate){
        Map<String,Object> m=new HashMap<String,Object>();
        JSONObject jsonObject=JSONObject.parseObject(istDate);
        Classroom clas=new Classroom();
        clas.setArea(jsonObject.getString("area"));
        clas.setBuildingNo(jsonObject.getString("buildingNo"));
        clas.setBuildingFloor(jsonObject.getString("buildingFloor"));
        clas.setClassroomId(jsonObject.getString("classroomId"));
        clas.setClassroomNumber(jsonObject.getString("classroomNumber"));
        clas.setClassroomName(jsonObject.getString("classroomName"));
        clas.setGalleryful(Integer.parseInt(jsonObject.getString("galleryful")));
        clas.setUseStatus(jsonObject.getString("useStatus"));
        clas.setDeleteFlag("0");


        Calendar now = Calendar.getInstance();//获取时间
        clas.setCreateTime(now.getTime());//当前时间
        clas.setUpdateTime(now.getTime());
        try {

           Integer dd=  classroomService.insertClass(clas);

            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"添加成功");
            m.put("data",dd);

        }catch (PhoenixException e){
            e.printStackTrace();
            log.error("exception::"+e.getMessage());
            log.error("exception--code"+e.getCode());
            log.error("exception--msg"+e.getMsg());
            m.put(MyContent.CODE,e.getCode());
            m.put(MyContent.MSG,e.getMsg());
            m.put("data",0);

        }
       return m;


    }


    @ResponseBody
    @RequestMapping("/deleteListClass")
    public Integer deleteListClass(@RequestBody String jsonStr){

        JSONObject jsonObject=JSONObject.parseObject(jsonStr);
        JSONArray pkId=jsonObject.getJSONArray("pkId");
        Integer[] pkid=new Integer[pkId.size()];
        for(int i=0;i<pkId.size();i++){
            Object id = pkId.get(i);
            pkid[i]=Integer.parseInt(id.toString());
            log.info("id————————————"+Integer.parseInt(id.toString()));
        }
        List<Integer> list= Arrays.asList(pkid);

        return classroomService.deleteListClass(list);
    }

    @ResponseBody
    @RequestMapping("/findRegion")
    public List<Classroom> findRegion(){
        return classroomService.findArea();
    }


    @ResponseBody
    @RequestMapping("/findBuildingNo")
    public List<Classroom> findBuildingNo(@RequestBody String sregion){
        JSONObject jsonObject=JSONObject.parseObject(sregion);
        String area=jsonObject.getString("area");
        return classroomService.findBuildingNo(area);
    }

    @ResponseBody
    @RequestMapping("/findBuildingFloor")
    public List<Classroom> findBuildingFloor(@RequestBody String rebNo){
        JSONObject jsonObject=JSONObject.parseObject(rebNo);
        String area=jsonObject.getString("area");
        String bNo=jsonObject.getString("buildingNo");
        return classroomService.findBuildingFloor(area,bNo);
    }



}
